﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArithmeticDemo
{
    public class LeeCode35
    {
        public void Start() {
            int[] nums = { 1, 3, 5, 6 };
            Console.WriteLine(SearchInsert(nums, 5));
            Console.WriteLine(SearchInsert(nums, 2));
            Console.WriteLine(SearchInsert(nums, 7));
            Console.WriteLine(SearchInsert(nums, 0));
            int[] nums2 = { 1 };
            Console.WriteLine(SearchInsert(nums2, 0));
        }
        public int SearchInsert(int[] nums, int target) {
            var left = 0;
            var right = nums.Length - 1;
            var i = 0;
            //var result = 0;
            while (left <= right) {
                i = left + (right - left) / 2;
                if (nums[i] < target) {
                    left = i + 1;
                } else{
                    right = i-1;
                } 
                
            }
            return left;
        }
    }
}
